package list

func getIntersectionNode(headA, headB *ListNode) *ListNode {
	nodeMap := map[*ListNode]struct{}{}

	pa, pb := headA, headB

	for pa != nil || pb != nil {
		if _, ok := nodeMap[pa]; ok && pa != nil {
			return pa
		} else if pa != nil {
			nodeMap[pa] = struct{}{}
			pa = pa.Next
		}

		if _, ok := nodeMap[pb]; ok && pb != nil {
			return pb
		} else if pb != nil {
			nodeMap[pb] = struct{}{}
			pb = pb.Next
		}
	}

	return nil
}
